package com.d16_Socket_Homework.homework2;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo1_TCP {
    public static void main(String[] args) throws IOException {
        //创建接收端Socket对象，指定接收端口号
        ServerSocket serverSocket = new ServerSocket(7777);
        //接收连接
        Socket accept = serverSocket.accept();
        System.out.println("服务端显示：");
        //创建输入流接收数据
        InputStream inputStream = accept.getInputStream();
        //获取发送端IP地址
        String host = accept.getInetAddress().getHostAddress();
        //创建指定长度字节数组接收数据
        byte[] bytes = new byte[8192];
        int len;
        while ((len=inputStream.read(bytes))!=-1){
            //打印输出字符串
            System.out.println(host+"说："+new String(bytes,0,len));
        }
    }
}
